package com.rongji.cms.tools.flowable.controller;

import com.rongji.cms.tools.flowable.domain.vo.FlowTaskVo;
import com.rongji.cms.tools.flowable.service.IWfInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 工作流流程实例管理
 *
 * @author yang
 * @create 2022/9/23
 **/
@Slf4j
//@Api(tags = "工作流流程实例管理")
@RestController
@RequestMapping("/workflow/instance")
public class WfInstanceController {

    @Autowired
    private IWfInstanceService flowInstanceService;

    /**
     * 根据流程定义id启动流程实例
     *
     * @param procDefId 流程定义id
     * @param variables 变量集合,json对象
     * @return
     */
    @PostMapping("/startBy/{procDefId}")
    public String startById(@PathVariable(value = "procDefId") String procDefId,
                            @RequestBody Map<String, Object> variables) {
        return flowInstanceService.startProcessInstanceById(procDefId, variables);

    }


    /**
     * 激活或挂起流程实例
     *
     * @param state      1:激活,2:挂起
     * @param instanceId 流程实例ID
     * @return
     */
    @PostMapping(value = "/updateState")
    public String updateState(@RequestParam Integer state,
                              @RequestParam String instanceId) {
        flowInstanceService.updateState(state, instanceId);
        return "操作成功";
    }

    /**
     * 结束流程实例
     *
     * @param flowTaskVo
     * @return
     */
    @PostMapping(value = "/stopProcessInstance")
    public String stopProcessInstance(@RequestBody FlowTaskVo flowTaskVo) {
        flowInstanceService.stopProcessInstance(flowTaskVo);
        return "结束流程实例成功";
    }

    /**
     * 删除流程实例
     *
     * @param instanceId   流程实例ID
     * @param deleteReason 删除原因
     * @return
     */
    @DeleteMapping(value = "/delete")
    public String delete(@RequestParam String instanceId,
                         @RequestParam(required = false) String deleteReason) {
        flowInstanceService.delete(instanceId, deleteReason);
        return "删除流程实例成功";
    }
}